//
//  HWGameGiftBagAlert.swift
//  HWPromoteProjectAPP
//
//  Created by 王昌健 on 2022/1/30.
//  Copyright © 2022 王昌健. All rights reserved.
//

import UIKit
import MMPopupView
import YLProgressBar
class HWGameGiftBagAlert: MMPopupView {

    private var tapBgView:UIView!
    private var bgView: UIView!
    var gameImageView:UIImageView!
    var nameLabel:UILabel!
    var progressBar:YLProgressBar!
    var surplusLabel:UILabel!
    var contentBgView:UIView!
    var contentLabel:UILabel!
    var copyButton:UIButton!
    var cancelButton:UIButton!
    var model:HWGameGiftBagModel?{
        didSet{
            gameImageView.sd_setImage(with: URL(string: model?.bgimg ?? ""), completed: nil)
            nameLabel.text = model?.gname
            contentLabel.text = model?.content
            surplusLabel.text = "剩余 \(model?.bfb ?? "")"
            progressBar.progress = CGFloat(((model?.cate ?? 0) - (model?.shengyu ?? 0))/(model?.cate ?? 0))
        }
    }
    @objc var close:(()->())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        initUI()
        
        initFrame()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    

}
extension HWGameGiftBagAlert {
    private func initUI() -> Void {
        type = MMPopupType.alert
        backgroundColor = UIColor.clear
        attachedView = UIApplication.shared.windows.filter({ $0.isKeyWindow }).last?.rootViewController?.view
        
        tapBgView = UIView.init()
        tapBgView.backgroundColor = UIColor.clear
        addSubview(tapBgView)
        let tap = UITapGestureRecognizer.init(target: self, action: #selector(bacgroundTap(_:)))
        tapBgView.addGestureRecognizer(tap)
        
        bgView = UIView()
        bgView.backgroundColor = .white
        bgView.layer.cornerRadius = 16*widthScale
        addSubview(bgView)
        
        gameImageView = UIImageView()
        gameImageView.backgroundColor = .red
        gameImageView.layer.cornerRadius = 12*widthScale
        gameImageView.layer.masksToBounds = true
        bgView.addSubview(gameImageView)
        
        nameLabel = HWLabel.createBoldLabel(title: "名字", color: "#444444", font: 18*widthScale)
        bgView.addSubview(nameLabel)
        
        progressBar = YLProgressBar()
        progressBar.indicatorTextDisplayMode = .none
        progressBar.progressTintColors = [UIColor.colorWithHexString(hexString: "#FFB60A"),UIColor.colorWithHexString(hexString: "#F66A05")]
        progressBar.trackTintColor = .colorWithHexString(hexString: "#EEEEEE")
        progressBar.cornerRadius = 0
        progressBar.hideStripes = true
        progressBar.progress = 0.88
        progressBar.hideGloss = true
        progressBar.progressStretch = true
        progressBar.progressBarInset = 0
        bgView.addSubview(progressBar)
        
        surplusLabel = HWLabel.createLabel(title: "剩余：12%", color: "#888888", font: 10*widthScale)
        bgView.addSubview(surplusLabel)
        
        contentBgView = UIView()
        contentBgView.backgroundColor = .colorWithHexString(hexString: "#F5F5F5")
        bgView.addSubview(contentBgView)
        
        contentLabel = HWLabel.createLabel(title: "礼包介绍礼包介绍礼包礼包介绍礼包介绍礼包介绍礼包介礼包介绍礼包介绍礼包介绍礼包介礼包介绍礼包介绍礼包介绍礼包介礼包介绍礼包介绍礼包介绍礼包介礼包介绍礼包介绍礼包介绍礼包介礼包介绍礼包介绍礼包介绍礼包介礼包介绍礼包介绍礼包介绍礼包介礼包介绍礼包介绍礼包介绍礼包介礼包介1", color: "#888888", font: 12*widthScale)
        contentBgView.addSubview(contentLabel)
        
        copyButton = UIButton(type: .custom)
        copyButton.setTitle("复制推广", for: .normal)
        copyButton.titleLabel?.font = .boldSystemFont(ofSize: 14*widthScale)
        copyButton.setTitleColor(.white, for: .normal)
        copyButton.backgroundColor = .colorWithHexString(hexString: "#5879FF")
        copyButton.layer.cornerRadius = 12*widthScale
        copyButton.layer.masksToBounds = true
        copyButton.addTarget(self, action: #selector(copyButtonClick(_:)), for: .touchUpInside)
        bgView.addSubview(copyButton)
        
        cancelButton = UIButton(type: .custom)
        cancelButton.setImage(UIImage(named: "cancelButton"), for: .normal)
        cancelButton.addTarget(self, action: #selector(cancelButtonClick(_:)), for: .touchUpInside)
        bgView.addSubview(cancelButton)
    }
    
    private func initFrame() -> Void {
        snp.makeConstraints { make in
            make.width.equalTo(screenWidth)
            make.height.equalTo(screenHeight)
        }
        
        tapBgView.snp.makeConstraints { make in
            make.top.left.right.bottom.equalTo(0)
        }
        
        bgView.snp.makeConstraints { make in
            make.left.equalTo(20*widthScale)
            make.right.equalTo(-20*widthScale)
            make.top.equalTo(148*widthScale)
        }
        
        gameImageView.snp.makeConstraints { make in
            make.left.top.equalTo(20*widthScale)
            make.width.height.equalTo(52*widthScale)
        }
        
        nameLabel.snp.makeConstraints { make in
            make.centerY.equalTo(gameImageView)
            make.left.equalTo(gameImageView.snp.right).offset(8*widthScale)
        }
        
        progressBar.snp.makeConstraints { make in
            make.left.equalTo(20*widthScale)
            make.top.equalTo(gameImageView.snp.bottom).offset(12*widthScale)
            make.height.equalTo(10*widthScale)
            make.width.equalTo(212*widthScale)
        }
        
        surplusLabel.snp.makeConstraints { make in
            make.left.equalTo(progressBar.snp.right).offset(13*widthScale)
            make.centerY.equalTo(progressBar)
        }
        
        contentBgView.snp.makeConstraints { make in
            make.left.equalTo(20*widthScale)
            make.right.equalTo(-20*widthScale)
            make.top.equalTo(progressBar.snp.bottom).offset(12*widthScale)
        }
        
        contentLabel.snp.makeConstraints { make in
            make.top.left.equalTo(10*widthScale)
            make.right.bottom.equalTo(-10*widthScale)
        }
        
        copyButton.snp.makeConstraints { make in
            make.top.equalTo(contentBgView.snp.bottom).offset(20*widthScale)
            make.left.equalTo(20*widthScale)
            make.right.bottom.equalTo(-20*widthScale)
            make.height.equalTo(44*widthScale)
        }
        
        cancelButton.snp.makeConstraints { make in
            make.right.equalTo(-20*widthScale)
            make.top.equalTo(20*widthScale)
            make.width.height.equalTo(16*widthScale)
        }
        
        
    }
}

extension HWGameGiftBagAlert {
    @objc func bacgroundTap(_ tap:UITapGestureRecognizer) -> Void {
        
        hide()
        
    }
    
    @objc func cancelButtonClick(_ button:UIButton) -> Void {
        hide()
    }
    
    @objc func copyButtonClick(_ button:UIButton) -> Void {
        let pboard = UIPasteboard.general
        pboard.string = model?.url
        showHUD(title: "复制成功")
    }
}
